﻿using System;
using System.IO;
using System.Windows.Forms;
using docNET.Common;
using docNET.Resources;

namespace docNET
{
	static class Program
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main()
		{
			Initialize();
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new docNetForm());
		}

		private static void Initialize()
		{
			var settings = docNET.Properties.Settings.Default;
			Current.Refresh();

			if (!settings.RunInitial)
				return;

			string projectPath = string.Format("{0}\\{1}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), settings.InitialProjectPath);

			try
			{
				if (!Directory.Exists(projectPath))
					Directory.CreateDirectory(projectPath);

				settings.DefaultProjectPath = projectPath;
			}
			catch
			{
				try // Failed to set project path, trying to set it to "My documents" instead.
				{
					settings.DefaultProjectPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
					DnMessageBox.Show(Messages.WarningSetDefaultProjectFolderFailed, MessageBoxIcon.Warning);
				}
				catch (Exception ex)
				{
					DnMessageBox.Show(Messages.ExceptionSetupFailed, MessageBoxIcon.Error, ex);
					Application.Exit();
				}
			}

			settings.RunInitial = false;
			settings.Save();
			Current.Refresh();
		}
	}
}
